package com.itheima.controller;

import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.itheima.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/minio")
//搭建FastDFS,用于文件存储
public class MinioController {
    @Autowired
    private FastFileStorageClient client;

    @Autowired
    private FdfsWebServer webServer;// 获取存储服务器的请求URL

    @PostMapping("/upload")
    public ResponseEntity upload(MultipartFile file) throws IOException {
        //2、视频上传到fdfs上，获取请求地址
        //获取文件的后缀名
        String filename = file.getOriginalFilename();  //xxxx.jpg
        String sufix = filename.substring(filename.lastIndexOf(".") + 1);//jpg
        StorePath storePath = client.uploadFile(file.getInputStream(),
                file.getSize(), sufix, null);//文件输入流，文件长度，文件后缀，元数据
        String fileUrl = webServer.getWebServerUrl() + storePath.getFullPath();
        return ResponseEntity.ok(Result.success("上传文件成功"));
    }

    @PostMapping("/delete")
    public ResponseEntity delete(String objectName) {
        client.deleteFile(objectName);
        return ResponseEntity.ok(Result.success("删除文件成功"));

    }
}
